package com.alarmgame.webview;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JsResult;
import android.webkit.WebChromeClient;
import android.webkit.WebView;
import android.widget.Toast;

import com.alarmgame.R;
import com.alarmgame.application.MainApplication;


public class AlarmWebView extends Activity{
	private WebView  mWebView;
    private static String URL = "http://www.meizitanggdl.com/HangedGame/";
    private boolean alarmOff = false;
	
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		
	    setContentView(R.layout.alert_web);
		mWebView = (WebView) findViewById(R.id.webview_compontent);
		mWebView.getSettings().setJavaScriptEnabled(true);
		mWebView.loadUrl(URL);
		mWebView.setWebChromeClient(new WebChromeClient() {
			@Override
			public boolean onJsAlert(WebView view, String url, String message, JsResult result){
				if(message.equals("true")){
					MainApplication mainApplication = ((MainApplication)getApplicationContext());
					mainApplication.getAlarm().stop();
					alarmOff = true;
					mainApplication.getAlertClose().finish();
				}
	            result.confirm();
	            return true;
			}
		});
		
		Toast.makeText(this, "AlarmWebView.onCreate()", Toast.LENGTH_LONG).show();
	}
	
	@Override
	protected void onDestroy() {
		super.onDestroy();
		if( !alarmOff ){
			Intent alarmWebView = new Intent(Intent.ACTION_VIEW);
			alarmWebView.setClass(getBaseContext(), AlarmWebView.class);
			alarmWebView.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
			startActivity(alarmWebView);
		}
		Toast.makeText(this, "AlarmWebView.onDestroy()", Toast.LENGTH_LONG).show();
	}
}